package com.song.tools.trigger;

import java.util.HashMap;
import java.util.Map;

/**
 * 触发器工厂
 *
 * @author song tools
 * @since 2024-07-19
 */
public class TriggerFactory {

    /**
     * 触发器映射
     */
    private static final Map<TriggerType, AbstractTriggerTemplate<?,?>> TRIGGER_MAP = new HashMap<>(16);

    /**
     * 注册触发器
     *
     * @param triggerType      触发器类型
     * @param triggerTemplate 触发器模板
     */
    public static <TriggerRequest, TriggerResponse> void register(TriggerType triggerType, AbstractTriggerTemplate<TriggerRequest, TriggerResponse> triggerTemplate) {
        TRIGGER_MAP.put(triggerType, triggerTemplate);
    }

    /**
     * 获取触发器
     *
     * @param triggerType 触发器类型
     * @return 触发器
     */
    @SuppressWarnings("unchecked")
    public static <TriggerRequest, TriggerResponse> AbstractTriggerTemplate<TriggerRequest, TriggerResponse> getTrigger(TriggerType triggerType) {
        return (AbstractTriggerTemplate<TriggerRequest, TriggerResponse>) TRIGGER_MAP.get(triggerType);
    }

    /**
     * 私有构造器
     */
    private TriggerFactory() {
    }
}
